<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['house_sale:ExamMaster:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['house_sale:ExamMaster:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['house_sale:ExamMaster:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['house_sale:ExamMaster:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="ExamMasterList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="examid" fixed/>
      <el-table-column label="试题序号" align="center" prop="unitno" />
      <el-table-column label="题目名称" align="center" prop="unitname" width="200px"/>
      <el-table-column label="题目类型" align="center" prop="unittype" />
      <el-table-column label="选项个数" align="center" prop="optcnt" />
      <el-table-column label="选项1" align="center" prop="value1" />
      <el-table-column label="选项2" align="center" prop="value2" />
      <el-table-column label="选项3" align="center" prop="value3" />
      <el-table-column label="选项4" align="center" prop="value4" />
      <el-table-column label="选项5" align="center" prop="value5" />
      <el-table-column label="选项6" align="center" prop="value6" />
      <el-table-column label="选项7" align="center" prop="value7" />
      <el-table-column label="选项8" align="center" prop="value8" />
    </el-table>
    
    <!-- 添加或修改答卷题目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :inline="true">
        <!-- <el-form-item label="试卷名称" prop="reseachid">
          <el-input v-model="form.reseachid" hidden/>
        </el-form-item> -->
        <el-form-item label="题目名称" prop="unitname">
          <el-input v-model="form.unitname" placeholder="请输入题目名称" />
        </el-form-item>
        <el-form-item label="试题序号" prop="unitno" >
          <el-input type="number" v-model="form.unitno" placeholder="请输入试题序号" min="1"/>
        </el-form-item>
        <el-form-item label="是否启用" prop="invalid">
           <el-select v-model="form.invalid" >
                <el-option v-for="i in invalids" :label="i.name" :value="i.name" :key="i.id"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="题目类型" prop="unittype">
           <el-select v-model="form.unittype" >
                <el-option v-for="u in unittypes" :label="u.name" :value="u.name" :key="u.id"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="选项个数" prop="optcnt">
          <el-select v-model="form.optcnt" placeholder="选项个数" >
          <el-option
              v-for="op in optcnts"
              :key="op"
              :label="op"
              :value="op"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="录入时间" prop="oprtime">
          <el-date-picker clearable
            v-model="form.oprtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择录入时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="选项1" prop="question1">
          <el-input v-model="form.question1" placeholder="请输入选项1" />
        </el-form-item>
        <el-form-item label="值1" prop="value1">
          <el-input v-model="form.value1" placeholder="请输入值1" />
        </el-form-item>
        <el-form-item label="选项2" prop="question2">
          <el-input v-model="form.question2" placeholder="请输入选项2" />
        </el-form-item>
        <el-form-item label="值2" prop="value2">
          <el-input v-model="form.value2" placeholder="请输入值2" />
        </el-form-item>
        <el-form-item label="选项3" prop="question3">
          <el-input v-model="form.question3" placeholder="请输入选项3" />
        </el-form-item>
        <el-form-item label="值3" prop="value3">
          <el-input v-model="form.value3" placeholder="请输入值3" />
        </el-form-item>
        <el-form-item label="选项4" prop="question4">
          <el-input v-model="form.question4" placeholder="请输入选项4" />
        </el-form-item>
        <el-form-item label="值4" prop="value4">
          <el-input v-model="form.value4" placeholder="请输入值4" />
        </el-form-item>
        <el-form-item label="选项5" prop="question5">
          <el-input v-model="form.question5" placeholder="请输入选项5" />
        </el-form-item>
        <el-form-item label="值5" prop="value5">
          <el-input v-model="form.value5" placeholder="请输入值5" />
        </el-form-item>
        <el-form-item label="选项6" prop="question6">
          <el-input v-model="form.question6" placeholder="请输入选项6" />
        </el-form-item>
        <el-form-item label="值6" prop="value6">
          <el-input v-model="form.value6" placeholder="请输入值6" />
        </el-form-item>
        <el-form-item label="选项7" prop="question7">
          <el-input v-model="form.question7" placeholder="请输入选项7" />
        </el-form-item>
        <el-form-item label="值7" prop="value7">
          <el-input v-model="form.value7" placeholder="请输入值7" />
        </el-form-item>
        <el-form-item label="选项8" prop="question8">
          <el-input v-model="form.question8" placeholder="请输入选项8" />
        </el-form-item>
        <el-form-item label="值8" prop="value8">
          <el-input v-model="form.value8" placeholder="请输入值8" />
        </el-form-item>
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listExamMaster, getExamMaster, delExamMaster, addExamMaster, updateExamMaster } from "@/api/house_sale/ExamMaster";

export default {
  name: "ExamMaster",
  dicts: ['sys_normal_disable', 'sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 答卷题目表格数据
      ExamMasterList: [],
      unittypes:[
         {id:0,name:'单选题'},
         {id:1,name:'多选题'},
         {id:2,name:'问答题'}
      ],
      
      invalids:[ {id:0,name:'启用'}, {id:1,name:'停用'}],
      optcnts:[
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        reseachid: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        unitname: [
          { required: true, message: "题目名称不能为空", trigger: "blur" }
        ],
          oprtime: [
          { required: true, message: "时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询答卷题目列表 */
    getList() {
      this.loading = true;
      const reseachid = this.$route.params && this.$route.params.reseachid;
      listExamMaster(reseachid).then(response => {
        console.log(reseachid)
        this.ExamMasterList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        examid: null,
        reseachid: this.$route.params.reseachid,
        unitno: null,
        unitname: null,
        invalid: '启用',
        unittype: '单选题',
        optcnt: 1,
        question1: null,
        question2: null,
        question3: null,
        question4: null,
        question5: null,
        question6: null,
        question7: null,
        question8: null,
        value1: null,
        value2: null,
        value3: null,
        value4: null,
        value5: null,
        value6: null,
        value7: null,
        value8: null,
        userid: this.$store.state.user.name,
        oprtime: null
      };
      this.resetForm("form");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.examid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加答卷题目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const examid = row.examid || this.ids
      getExamMaster(examid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改答卷题目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.examid != null) {
            updateExamMaster(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExamMaster(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const examids = row.examid || this.ids;
      this.$modal.confirm('是否确认删除答卷题目编号为"' + examids + '"的数据项？').then(function() {
        return delExamMaster(examids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
     
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('house_sale/ExamMaster/export', {
        ...this.queryParams
      }, `ExamMaster_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
